package com.example.udpscreen.servers;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.SocketException;

import org.greenrobot.eventbus.EventBus;

import com.example.udpscreen.enums.UdpSocketDataType;
import com.example.udpscreen.events.CmdEvent;
import com.example.udpscreen.runnables.RecvFileRunnable;
import com.example.udpscreen.runnables.RecvResumeFileRunnable;
import com.example.udpscreen.runnables.SendFileRunnable;





public class ADPlayerSocket extends BaseSocket {
	
	private RecvResumeFileRunnable mRecvResumeFileRunnable;
	private SendFileRunnable mSendFileRunnable;
	private RecvFileRunnable mRecvFileRunnable;
	public ADPlayerSocket(int bindPort) throws SocketException {
		super(bindPort);

	}

	public ADPlayerSocket(int bindPort, int sotimeout) throws SocketException {
		super(bindPort, sotimeout);

	}
	
	@Override
	public void startWork() {
		super.startWork();
	}

	@Override
	protected void dealWithData(DatagramPacket packet) {
		byte[] recv_buffer = packet.getData();
		int real_len = packet.getLength();
		//System.out.println(recv_buffer[2]);
		if (real_len > 2) {
			if (recv_buffer[0] + recv_buffer[1] == -1) {
				byte type = recv_buffer[0];
				if (type == UdpSocketDataType.COMMAND) { // 控制命令
					String str=new String(recv_buffer,10,real_len);
					
					_dealwithCommand(str,packet.getAddress(),packet.getPort());
				} else if (type == UdpSocketDataType.FILE_SEND) { // 文件发送
					if(mSendFileRunnable!=null&&mSendFileRunnable.getRunStatus())
						mSendFileRunnable.dealWithData(recv_buffer, real_len);
				}else if(type == UdpSocketDataType.FILE_RECIVE){
					if(mRecvFileRunnable!=null&&mRecvFileRunnable.getRunStatus())
						mRecvFileRunnable.whriteData(recv_buffer, real_len);
				}else if (type == UdpSocketDataType.FILE_RESUME_RECIVE) { // 文件断点续传接收
					if(mRecvResumeFileRunnable!=null&&mRecvResumeFileRunnable.getRunStatus())
						mRecvResumeFileRunnable.whriteData(recv_buffer,real_len);
				} else if(type ==UdpSocketDataType.FILE_RESUME_SEND){ //断点续传发送
					
				}
			}
			
			

		}

	}
	
	private void _dealwithCommand(final String str,InetAddress address, int port ){
		CmdEvent cmdEvent = new CmdEvent(address, port, str);
		EventBus.getDefault().post(cmdEvent);
//		JSONObject json;
//		try {
//			json = new JSONObject(str);
//			int cmd =json.getInt("cmd");
//			if(2==cmd){ //发送文件应答
//				String file_name=json.getString("file_name");
//				String file_path=json.getString("file_path");
//				String file_md5 =json.getString("file_md5");
//				long file_size=json.getLong("file_size");
//				RecvFileEvent event = new RecvFileEvent();
//				event.FileName=file_name;
//				event.FilePath=file_path;
//				event.Md5=file_md5;
//				event.mRemoteAddress=address;
//				event.mRemotePort=port;
//				event.FileSize=file_size;
//				EventBus.getDefault().post(event);
//				
//			}else if(3==cmd){
//				EventBus.getDefault().post(new CaptureEvent(address,port));
//			}
//		} catch (JSONException e) {
//			e.printStackTrace();
//		}
	}
	
	public void setRecvResumeFileRunnable(RecvResumeFileRunnable runnable){
		this.mRecvResumeFileRunnable = runnable;
	}
	
	public void setSendFileRunnable(SendFileRunnable runnable){
		this.mSendFileRunnable = runnable;
	}

	public void setRecvFileRunnable(RecvFileRunnable runnable){
		this.mRecvFileRunnable = runnable;
	}
	public void runScreenWatch() {

	}
	@Override
	protected void onRelease() {
		super.onRelease();
	}

}
